const path = require("path");
const Joi = require("@hapi/joi");
const handler = require(path.join(__dirname, "../services/tagService.js"));
const { validate } = require("../middlewares/validate");

module.exports = (router, base) => {
  router.get(
    `/api/${base}/getTagsList`,
    validate({
      query: Joi.object().keys({
        page: Joi.number().optional().description("页码"),
        pageSize: Joi.number().optional().description("页数"),
        search: Joi.string().allow("").optional().description("搜索内容"),
        created: Joi.number().optional().description("排序方式"),
      }),
    }),
    handler.getTagsList
  );

  router.post(
    `/api/${base}/addTag`,
    validate({
      payload: Joi.object().keys({
        name: Joi.string().required().description("标签名"),
        color: Joi.string().allow("").optional().description("标签颜色"),
        isIncome: Joi.boolean().required().description("是否是收入"),
      }),
    }),
    handler.addTag
  );

  router.post(
    `/api/${base}/editTag`,
    validate({
      payload: Joi.object().keys({
        id: Joi.string().required().description("标签id"),
        name: Joi.string().required().description("标签名"),
        color: Joi.string().allow("").optional().description("标签颜色"),
        isIncome: Joi.boolean().required().description("是否是收入"),
      }),
    }),
    handler.editTag
  );

  router.post(
    `/api/${base}/deleteTags`,
    validate({
      payload: Joi.object().keys({
        ids: Joi.array()
          .items(Joi.string().optional())
          .description("要删除的标签id集合"),
      }),
    }),
    handler.deleteTags
  );
};
